#include "../../Source/ObjMeshManager.h"
#include "../../Source/CameraManager.h"

#include "ClientPlayerManager.h"
#include "ClientManager.h"
#include "TimerMS.h"
#include "GMSetPosOnServer.h"
#include "SendGameMessage.h"

#include <iostream>
#include <algorithm>


ClientPlayerManager::ClientPlayerManager()
{
	m_sendPosTimer = 0;
}

void ClientPlayerManager::Draw()
{
	MutexLocker ml(&m_mutex);
	//D3DXVECTOR3 pos = TheCameraManager::Instance()->GetCameraPos();
	//TheObjMeshManager::Instance()->Translate("crate_textured", pos);
	//TheObjMeshManager::Instance()->Draw("crate_textured");

	for (auto it = m_clients.begin(); it != m_clients.end(); ++it)
	{
		if (std::find(m_removedList.begin(), m_removedList.end(), it->first) == m_removedList.end())
		{	
			TheObjMeshManager::Instance()->SetColour("crate_textured", D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f));
			D3DXVECTOR3 pos = D3DXVECTOR3(it->second.pos.x, it->second.pos.y, it->second.pos.z); 
			TheObjMeshManager::Instance()->Translate("crate_textured", pos);
			TheObjMeshManager::Instance()->Draw("crate_textured");
		}
	}

	auto it = m_clients.find(TheClientManager::Instance()->GetID());
	if (it != m_clients.end())
	{
		TheClientManager::Instance()->SetPos(it->second.pos);
		
		m_sendPosTimer += TheTimerMS::Instance()->GetDtMS();
		if (m_sendPosTimer > 5500)
		{
			GMSetPosOnServer* gm = new GMSetPosOnServer();
			gm->SetID(it->first);
			gm->SetPos(it->second.pos);

			//SendGameMessage(*gm, &TheClientManager::Instance()->GetSocket());

			m_sendPosTimer = 0;
			delete gm;
		}
	}
}

void ClientPlayerManager::Update()
{
	float dt = TheTimerMS::Instance()->GetDtMS() * 0.001f;
	static float decVel = 0.0f;
	decVel += dt;
	// capped to 75fps
	if (decVel > 0.0f)
	{
		for (auto it = m_clients.begin(); it != m_clients.end(); ++it)
		{
			//if (std::find(m_removedList.begin(), m_removedList.end(), it->first) == m_removedList.end())
			//{			
				it->second.pos += it->second.vel * dt;
				it->second.vel *= 0.99f;
			//}
		}
		decVel = 0.0f;
	}

	//auto it = m_clients.find(TheClientManager::Instance()->GetID());
	//if (it != m_clients.end())
	//{
	//	TheClientManager::Instance()->SetVel(it->second.vel);
	//}
}

void ClientPlayerManager::AddClient(int id, ClientInfo info)
{
	m_clients[id] = info;
}

void ClientPlayerManager::SetClientPos(int id, const Vec3fNet& pos)
{
	m_clients.find(id)->second.pos = pos;
}

Vec3fNet ClientPlayerManager::GetClientPos(int id) const
{
	return m_clients.find(id)->second.pos;
}

void ClientPlayerManager::SetClientVel(int id, const Vec3fNet& vel)
{
	m_clients.find(id)->second.vel = vel;
}

Vec3fNet ClientPlayerManager::GetClientVel(int id) const
{
	return m_clients.find(id)->second.vel;
}

void ClientPlayerManager::RemoveClient(int id)
{
	m_clients.erase(m_clients.find(id));
	m_removedList.push_back(id);
	std::cout << "client: " << id << " removed" << std::endl;
	std::cout << std::endl << std::endl << std::endl << std::endl;
}